﻿<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-tabs v-model="activeTab">
			<el-tab-pane label="基本信息" name="main">
				<el-card shadow="hover" class="layout-padding-auto create-form">
					<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
						<!-- 员工基本信息 - 优先级最高 -->
						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>员工基本信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Name_FormItem" prop="Entity.Name">
										<template #label>
											<WtmLabelTooltip label="姓名" tooltip="员工姓名" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Name" disabled clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_StaffNo_FormItem" prop="Entity.StaffNo">
										<template #label>
											<WtmLabelTooltip label="工号" tooltip="员工工号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.StaffNo" disabled clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Gender_FormItem" prop="Entity.Gender">
										<template #label>
											<WtmLabelTooltip label="性别" tooltip="员工性别" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.Gender" disabled clearable class="width100">
											<el-option v-for="item in GENDER_OPTIONS" :key="item" :value="item" :label="item"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Phone_FormItem" prop="Entity.Phone">
										<template #label>
											<WtmLabelTooltip label="手机号" tooltip="手机号码" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Phone" disabled clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<!-- 身份证号 -->
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_IdCardNo_FormItem" prop="Entity.IdCardNo">
										<template #label>
											<WtmLabelTooltip label="身份证号" tooltip="身份证号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.IdCardNo" disabled clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<!-- 出生日期 -->
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_BirthDate_FormItem" prop="Entity.BirthDate">
										<template #label>
											<WtmLabelTooltip label="出生日期" tooltip="出生日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.BirthDate" disabled clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<!-- 所属部门 -->
								<el-col :xs="24" :lg="12" class="mb20">
									<WtmDeptSelector v-model:model-value="stateData.vmModel.Entity.GroupId" prop="Entity.GroupId" label="所属部门" :disabled="true" />
								</el-col>

								<!-- 公司 -->
								<el-col :xs="24" :lg="12" class="mb20">
									<WtmOrgSelector v-model="stateData.vmModel.Entity.OrgId" label="公司" prop="Entity.OrgId" :disabled="true" />
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Email_FormItem" prop="Entity.Email">
										<template #label>
											<WtmLabelTooltip label="邮箱" tooltip="电子邮箱" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Email" disabled clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<!-- 转正基本信息 - 第二优先级 -->
						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>转正基本信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_EntryDate_FormItem" prop="Entity.EntryDate">
										<template #label>
											<WtmLabelTooltip label="入职日期" tooltip="入职日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.EntryDate" disabled clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_RegDate_FormItem" prop="Entity.RegDate">
										<template #label>
											<WtmLabelTooltip label="转正日期" tooltip="转正日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.RegDate" disabled clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<!-- 试用期信息 - 第三优先级 -->
						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>试用期信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ProbationStartDate_FormItem" prop="Entity.ProbationStartDate">
										<template #label>
											<WtmLabelTooltip label="试用期起始日期" tooltip="试用期起始日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ProbationStartDate" disabled clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ProbationEndDate_FormItem" prop="Entity.ProbationEndDate">
										<template #label>
											<WtmLabelTooltip label="试用期结束日期" tooltip="试用期结束日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ProbationEndDate" disabled clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<!-- <el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ProbationSalary_FormItem" prop="Entity.ProbationSalary">
										<template #label>
											<WtmLabelTooltip label="试用期薪资" tooltip="试用期薪资" />
										</template>
										<el-input-number v-model="stateData.vmModel.Entity.ProbationSalary" disabled clearable class="width100"></el-input-number>
									</el-form-item>
								</el-col> -->
							</el-row>
						</div>

						<!-- 转正薪资信息 - 第四优先级 -->
						<!-- <div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>转正薪资信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_RegularSalary_FormItem" prop="Entity.RegularSalary">
										<template #label>
											<WtmLabelTooltip label="转正后薪资" tooltip="转正后薪资" />
										</template>
										<el-input-number v-model="stateData.vmModel.Entity.RegularSalary" disabled clearable class="width100"></el-input-number>
									</el-form-item>
								</el-col>
							</el-row>
						</div> -->

						<!-- 转正评估内容 - 第五优先级 -->
						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>转正评估内容</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="24" class="mb20">
									<el-form-item ref="Entity_JobContent_FormItem" prop="Entity.JobContent">
										<template #label>
											<WtmLabelTooltip label="试用期工作内容" tooltip="试用期主要工作内容描述" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.JobContent" type="textarea" :rows="3" disabled clearable></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="24" class="mb20">
									<el-form-item ref="Entity_JobAchievements_FormItem" prop="Entity.JobAchievements">
										<template #label>
											<WtmLabelTooltip label="试用期工作成果" tooltip="试用期取得的主要工作成果" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.JobAchievements" type="textarea" :rows="3" disabled clearable></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="24" class="mb20">
									<el-form-item ref="Entity_RegOpinion_FormItem" prop="Entity.RegOpinion">
										<template #label>
											<WtmLabelTooltip label="转正意见" tooltip="转正申请意见" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.RegOpinion" type="textarea" :rows="3" disabled clearable></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="24" class="mb20">
									<el-form-item ref="Entity_Remarks_FormItem" prop="Entity.Remarks">
										<template #label>
											<WtmLabelTooltip label="其他说明" tooltip="其他需要说明的事项" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Remarks" type="textarea" :rows="3" disabled clearable></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<!-- 单据信息 - 最后展示 -->
						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>单据信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_BillCode_FormItem" prop="Entity.BillCode">
										<template #label>
											<WtmLabelTooltip label="单据编号" tooltip="单编号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.BillCode" disabled clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Remark_FormItem" prop="Entity.Remark">
										<template #label>
											<WtmLabelTooltip label="备注" tooltip="备注" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Remark" disabled clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<!-- 流程信息 -->
						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>流程信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Approver_FormItem" prop="Entity.Approver">
										<template #label>
											<WtmLabelTooltip label="审批人" tooltip="流程审批人" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Approver" disabled clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ApprovalTime_FormItem" prop="Entity.ApprovalTime">
										<template #label>
											<WtmLabelTooltip label="审批时间" tooltip="流程审批时间" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ApprovalTime" disabled clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<el-row :gutter="20"></el-row>
					</el-form>
				</el-card>
			</el-tab-pane>
			<!-- <el-tab-pane label="学习经历" name="personeducations" v-if="stateData.vmModel.Entity.ID">
				<SysPersonEducation :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="家庭成员" name="personfamilies" v-if="stateData.vmModel.Entity.ID">
				<SysPersonFamily :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="工作经历" name="personworks" v-if="stateData.vmModel.Entity.ID">
				<SysPersonWork :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="培训考试" name="persontrainexams" v-if="stateData.vmModel.Entity.ID">
				<SysPersonTrainExam :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="员工体检" name="personmedicals" v-if="stateData.vmModel.Entity.ID">
				<SysPersonMedical :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="员工履历" name="personresumes" v-if="stateData.vmModel.Entity.ID">
				<SysPersonResume :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="员工合同" name="personcontracts" v-if="stateData.vmModel.Entity.ID">
				<SysPersonContract :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane> -->
			<el-tab-pane label="审批信息" key="审批信息">
				<WtmWorkflowTimeline entityType="HengTong.Model.Db.Bill.SysHrProbationRequestBill" :entityId="stateData.vmModel.Entity.ID" />
			</el-tab-pane>
		</el-tabs>
	</div>
</template>

<script setup lang="ts" name="message._system.common.vm.edit;false">
import { reactive, ref, getCurrentInstance, onMounted, nextTick } from 'vue';
import { ElMessage } from 'element-plus';
import { SysHrProbationRequestBillApi as api } from '/@/views/hr/syshrprobationrequestbill/api';
import other from '/@/utils/other';
import { commonApi } from '/@/api/common';
import { useRouter } from 'vue-router';
import { ISysHrProbationRequestBillVm } from '/@/views/hr/syshrprobationrequestbill/api/interface';
import { IViewModel } from '/@/types/common';
import ApproveReasonDialog from '/@/components/approveReason/index.vue';

import SysPersonEducation from '/@/views/person/syspersoneducation/index.vue';
import SysPersonFamily from '/@/views/person/syspersonfamily/index.vue';
import SysPersonWork from '/@/views/person/syspersonwork/index.vue';
import SysPersonTrainExam from '/@/views/person/syspersontrainexam/index.vue';
import SysPersonMedical from '/@/views/person/syspersonmedical/index.vue';
import SysPersonResume from '/@/views/person/syspersonresume/index.vue';
import SysPersonContract from '/@/views/person/syspersoncontract/index.vue';

// 常量定义
const ACTIONS = {
	AGREE: '同意',
	REJECT: '拒绝',
	CLOSE: '关闭',
} as const;

const ci = getCurrentInstance() as any;
const loading = ref(false);
// 定义变量内容
const formRef = ref();
const stateData = reactive<IViewModel<ISysHrProbationRequestBillVm>>({
	vmModel: {
		Entity: {
			ID: null,
			EntryDate: null,
			RegDate: null,
			ProbationStartDate: null,
			ProbationEndDate: null,
			ProbationSalary: null,
			RegularSalary: null,
			JobContent: null,
			JobAchievements: null,
			Remarks: null,
			RegOpinion: null,
			Status: null,
			RecruitNo: null,
			ArrivalDate: null,
			CurrentSalary: null,
			ExpectedSalary: null,
			WorkExperience: null,
			InitialTestTime: null,
			InitialResult: null,
			InitialComment: null,
			ReExamTime: null,
			ReExamResult: null,
			ReExamComment: null,
			FinalResult: null,
			FinalRemark: null,
			RejectReason: null,
			BillCode: null,
			BillStatus: null,
			WorkflowStatus: null,
			Remark: null,
			Approver: null,
			ApprovalTime: null,
			Name: null,
			StaffNo: null,
			BirthDate: null,
			Age: null,
			Gender: null,
			Ethnicity: null,
			HighestEducation: null,
			MaritalStatus: null,
			PhotoId: null,
			IdCardNo: null,
			IdCardFrontPhotoId: null,
			IdCardBackPhotoId: null,
			HouseholdRegistration: null,
			NativePlace: null,
			PoliticalStatus: null,
			PartyJoinDate: null,
			PartyBranch: null,
			ArchiveLocation: null,
			ArchiveNo: null,
			ArchiveTransferOutDate: null,
			ArchiveTransferInDate: null,
			HomeAddress: null,
			Phone: null,
			Email: null,
			SalaryBank: null,
			SalaryAccount: null,
			BonusBank: null,
			BonusAccount: null,
			OrgId: null,
			GroupId: null,
			JobPositionId: null,
			ResumeId: null,
			HireDate: null,
			ConfirmationDate: null,
			EmploymentType: null,
			ContractType: null,
			ContractNo: null,
			ContractStartDate: null,
			ContractEndDate: null,
			ResignationDate: null,
			ResignationReason: null,
			ResignationType: null,
			State: null,
			BloodType: null,
			Hobbies: null,
		},
		ActionName: null,
		IsMyApprove: false,
		Remark: null,
	},

	AllSysOrgs: [] as any[],
	AllSysGroups: [] as any[],
	AllSysJobPositions: [] as any[],
});

// 计算属性（已删除不需要的计算属性）

// 统一的成功处理函数
const handleSuccess = () => {
	ElMessage.success(ci.proxy?.$t('message._system.common.vm.submittip') || '操作成功');
	emit('refresh');
	closeDialog();
	loading.value = false;
};

// 统一的错误处理函数
const handleError = (error: any) => {
	other.setFormError(ci, error);
	loading.value = false;
};

// 公共的审批处理函数
const handleApproval = (actionName: string) => {
	stateData.vmModel.ActionName = actionName;
	loading.value = true;
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			api()
				.approve(stateData.vmModel, '')
				.then(() => handleSuccess())
				.catch(handleError);
		} else {
			loading.value = false;
		}
	});
};

// 同意
const onAgree = () => {
	other.openDialog('填写同意原因', ApproveReasonDialog, null, null, '30%', (reason: string) => {
		stateData.vmModel.Remark = reason;
		handleApproval(ACTIONS.AGREE);
	});
};

// 拒绝
const onDeny = () => {
	other.openDialog('填写拒绝原因', ApproveReasonDialog, { isRequired: true }, null, '30%', (reason: string) => {
		stateData.vmModel.Remark = reason;
		handleApproval(ACTIONS.REJECT);
	});
};

const activeTab = ref('main');

const GENDER_OPTIONS = ['男', '女', '其他'] as const;

// 获取当前登录人信息
const getCurrentPersonInfo = async () => {
	try {
		const response = await commonApi().getPersonInfo({});
		if (response && response.Data) {
			const personInfo = response.Data;
			// 自动填充当前登录人的信息
			stateData.vmModel.Entity.Name = personInfo.Name || '';
			stateData.vmModel.Entity.StaffNo = personInfo.StaffNo || '';
			stateData.vmModel.Entity.Gender = personInfo.Gender || '';
			stateData.vmModel.Entity.Phone = personInfo.Phone || '';
			stateData.vmModel.Entity.Email = personInfo.Email || '';
			stateData.vmModel.Entity.GroupId = personInfo.GroupId || null;
			stateData.vmModel.Entity.IdCardNo = personInfo.IdCardNo || '';
			stateData.vmModel.Entity.BirthDate = personInfo.BirthDate || null;
		}
	} catch (error) {
		console.warn('获取当前登录人信息失败:', error);
	}
};

// 初始化页面数据
const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	const routeId = useRouter().currentRoute.value.query.id;

	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
	} else if (routeId) {
		stateData.vmModel.Entity.ID = routeId as any;
	}
};

// 加载页面数据
const loadPageData = async () => {
	const [entityData] = await Promise.allSettled([api().get(stateData.vmModel.Entity.ID ?? '')]);

	// 处理结果
	if (entityData.status === 'fulfilled') {
		other.setValue(stateData.vmModel, entityData.value);
	}
};

// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		await loadPageData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 关闭弹窗
const onClose = () => {
	emit('closeDialog');
};

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);
// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};
// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: '同意',
			type: 'primary', // 默认样式，简约灰色
			onClick: onAgree,
			visible: stateData.vmModel.IsMyApprove,
		},
		{
			text: '拒绝',
			type: 'danger', // 主要操作，醒目蓝色
			onClick: onDeny,
			visible: stateData.vmModel.IsMyApprove,
		},
		{
			text: '关闭',
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];
	// 通知父对话框更新头部按钮
	emit('updateHeaderButtons', headerButtons);
};
</script>
